/**
 * By wangmingxue
 * Created by wangmingxue on 16/05/01.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'fineuploader',
    'calendar'
], function ($, common, ready, Collection, template, Model, dialog, fineuploader, calendar) {
    var index = function () {
        this.init();
    };
    index.prototype = {
        init: function () {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function () {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'data/order_detailed',
                condition: {limit: 10, offset: 0},
                model: {
                    tag_name: 'tr',
                    url: {},
                    event: {
                        '.btn-add_user_tag': 'add_user_tag'
                    },
                    add_user_tag: function (e) {
                        var Tthis = this;
                        var date_time = Tthis.data.date_time;
                        $.ajax({
                            url: _global.url.api + 'data/get_order_detailed',
                            data: {date_time: date_time},
                            type: 'get',
                            async: false,
                            success: function (res) {
                                dialog({
                                    title: '商品成交明细',
                                    width: 600,
                                    modal: true,
                                    content: template('add-user-tag')
                                }).show();
                                if (res.code == 0) {
                                    var str = '';
                                    for (var i in res.data) {
                                        var temp = res.data[i];
                                        if (!!temp.record) {
                                            str += '<tr><td>' + temp.record + '</td><td>' + temp.page_url + '</td><td>' + temp.region + '</td><td>' + temp.card_id + '</td></tr>';
                                        }
                                    }
                                    $("#add-data-list").html(str);
                                }
                            }
                        });
                    }
                },
                callback: function () {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function () {
            var _this = this;
            //日期
            $('.form_datetime').calendar();
            //按照日期和标签查询
            $('#btn-search').on('click', function (e) {
                e.preventDefault();
                _this.collection.condition.start_time = $('.start_time').val();
                _this.collection.condition.end_time = $('.end_time').val();
                _this.collection.reload();
            });
        }
    };
    new index();
});